package com.yinhe.music.yhmusic.db.dao;

import com.j256.ormlite.dao.Dao;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.db.AbstractBaseDb;
import com.yinhe.music.yhmusic.db.DbHelper;
import com.yinhe.music.yhmusic.db.bean.SongDBEntity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by wangyang on 17/3/18.
 */

public class SongEntityDAO extends AbstractBaseDb {

    private static final String TAG = SongEntityDAO.class.getSimpleName();
    private Dao dao;


    public SongEntityDAO(DbHelper dbHelper) {
        super(dbHelper);
        dao = getDao(SongDBEntity.class);
    }

    public void insertSongList(final List<SongDBEntity> songDBEntities) {
        try {
            for (SongDBEntity i : songDBEntities) {
                dao.createOrUpdate(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insertSong(final SongDBEntity songDBEntity) {
        try {
            dao.createIfNotExists(songDBEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateSong(final SongDBEntity songDBEntity) {
        try {
            dao.update(songDBEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateSongList(final List<SongDBEntity> songDBEntitys) {
        try {
            for (SongDBEntity i : songDBEntitys) {
                dao.update(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<SongDBEntity> getSongByIsLike(final int isLike) {
        try {
            List<SongDBEntity> list = (List<SongDBEntity>) dao.queryForEq(SongDBEntity.SongStoreColumns.IS_LIKE, isLike);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }


    public List<SongDBEntity> getSongListByIdArray(final List<Integer> songIds) {
        try {
            List<SongDBEntity> info = dao.queryBuilder().where().in(SongDBEntity.SongStoreColumns.SONG_ID, songIds).query();
            return info;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }


    public SongDBEntity getSongBySongIdSyn(final long songID) {
        try {
            return (SongDBEntity) dao.queryForId(songID);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public String getQtypeBySongIdSyn(final long songID) {
        try {
            SongDBEntity songDBEntity = (SongDBEntity) dao.queryForId(songID);
            return songDBEntity.getQtype();
        } catch (Exception e) {
            e.printStackTrace();
            return IConstants.Common;
        }
    }

    public List<SongDBEntity> getSongLsitBySongIdSyn(final List<Long> songIDList) {
        try {
            List<SongDBEntity> songDBEntities = new ArrayList<>();
            for (Long i : songIDList) {
                SongDBEntity songDBEntity = (SongDBEntity) dao.queryForId(i);
                songDBEntities.add(songDBEntity);
            }
            return songDBEntities;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean isExistId(int songId) {
        try {
            SongDBEntity songDBEntity = (SongDBEntity) dao.queryForId(songId);
            if (songDBEntity != null) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
